//
// Created by Jisam on 2024/8/11 21:05.
// solve P1902 刺杀大使
// 08-11 21:25:21 AC
#include <bits/stdc++.h>

using namespace std;
int dx[] = {-1, 1, 0, 0,};
int dy[] = {0, 0, -1, 1,};

bool bfs(int x, int y, int m, int n, vector<vector<int >> &vis, int mid, vector<vector<int >> &a) {
    queue<pair<int, int>> q;
    q.push({x, y});
    vis[x][y] = 1;
    while (!q.empty()) {
        auto [i, j] = q.front();
        q.pop();
        if (i == n - 1 ) {
            return true;
        }
        for (int k = 0; k < 4; k++) {
            int tx = i + dx[k];
            int ty = j + dy[k];
            if (tx >= 0 && ty >= 0 && tx < n && ty < m && vis[tx][ty] == 0 && a[tx][ty] <= mid) {
//                cerr << tx <<" " <<ty <<"\n";
                vis[tx][ty] = 1;
                q.push({tx, ty});
            }
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<vector<int >> a(n, vector<int>(m, 0));
    int l = INT_MAX, r = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
            r = max(r, a[i][j]);
            l = min(l, a[i][j]);
        }
    }
    int ans = 0;
    while (l <= r) {
        int mid = (l + r) / 2;
        int f = 0;
        vector<vector<int >> vis(n, vector<int>(m, 0));
        if (bfs(1, 1, m, n, vis, mid, a)) {
            r = mid - 1;
            ans = mid;
        } else {
            l = mid + 1;
        }
    }
    cout << ans;

    return 0;
}